/*
 * @Descripttion: 
 * @Author: Lius
 * @version: 
 * @Date: 2023-02-12 00:00:39
 * @LastEditors: Lius
 * @LastEditTime: 2023-02-12 14:29:26
 */
let baseUrl_ = ''
// baseUrl_ = 'http://192.168.5.88:8086';
//测试服
// baseUrl_ = 'http://42.193.23.118:8086'
// baseUrl_ = 'http://yizhi.mlscd.cn/prod-api';
//正式服
baseUrl_ = 'https://admin.yz-future.com/prod-api';
// 分享链接
// http://yizhishare.mlscd.cn/#
export let routeBaseUrl = 'http://share.yz-future.com/#'

export const baseUrl = baseUrl_
export const ajax = (option) => {
	const contentType = 'application/x-www-form-urlencoded';
	const token = uni.getStorageSync('token')
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + option.url,
			data: option.data || {},
			method: option.method,
			timeout: 1000 * 6,
			header: {
				"Content-Type": option.contentType ? option.contentType : contentType,
				'known-session': token,
			},
			success: (res) => {
				var pages = getCurrentPages()
				let currentPage = pages[pages.length - 1].route
				let curParams = pages[pages.length - 1].options
				let params = ''
				for(let key in curParams){
					params += '&'+key+'='+ curParams[key]
				}
				let curpath = currentPage + '?' + params.slice(1)
				if (res.data.code >= 200 && res.data.code < 300) {
					resolve(res.data);
				} else {
					if (res.data.code == 401) {
						uni.clearStorageSync()
						uni.showToast({
							title: '登录已过期,请重新登录或授权',
							icon: 'none',
							mask: true
						})
						uni.setStorageSync('currentPage',curpath)
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/login/login'
							})
						}, 1000)
					} else if (res.data.code == 1001) {
						uni.clearStorageSync()
						uni.showToast({
							title: '登录超时,请重新登录或授权',
							icon: 'none',
							mask: true
						})
						setTimeout(() => {
							uni.setStorageSync('currentPage',curpath)
							uni.redirectTo({
								url: '/pages/login/login'
							})
						}, 1000)
					} else if (res.data.code == 403) {
						uni.showToast({
							title: '用户操作没有权限',
							icon: 'none'
						})
						resolve(res.data);
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.msg || res.msg
						})
						reject(res.data);
					}
				}
			},
			fail: err => {
				uni.showToast({
					icon: 'error',
					title: '请求失败'
				})
				reject(err);
			},
		});
	})
}
